﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dundas.Charting.WebControl;
using System.Data;
using System.Drawing;
using Ths.Components.Config;

public partial class UCChart_UCChart : System.Web.UI.UserControl
{

    #region 图表标题
    private string _title;
    /// <summary>
    /// 图表标题
    /// </summary>
    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }
    #endregion

    #region 图表宽度
    private int _width = 255;
    /// <summary>
    /// 图表宽度
    /// </summary>
    public int Width
    {
        get { return _width; }
        set { _width = value; }
    }
    #endregion

    #region 显示列1
    private int _column1 = 0;
    /// <summary>
    /// 图表宽度
    /// </summary>
    public int Column1
    {
        get { return _column1; }
        set { _column1 = value; }
    }
    #endregion

    #region 显示列2
    private int _column2 =1;
    /// <summary>
    /// 图表宽度
    /// </summary>
    public int Column2
    {
        get { return _column2; }
        set { _column2 = value; }
    }
    #endregion


    #region 图表高度
    private int _height = 147;
    /// <summary>
    /// 图表高度
    /// </summary>
    public int Height
    {
        get { return _height; }
        set { _height = value; }
    }
    #endregion

    #region 图表数据源
    private DataTable _dtSource;
    /// <summary>
    /// 图表数据源
    /// </summary>
    public DataTable DtSource
    {
        get { return _dtSource; }
        set { _dtSource = value; }
    }
    #endregion

    #region 图表类型
    private SeriesChartType _chartType;
    /// <summary>
    /// 图表类型
    /// </summary>
    public SeriesChartType ChartType
    {
        get { return _chartType; }
        set { _chartType = value; }
    }
    #endregion

    #region 是否3D显示
    private bool _is3DStyle;
    /// <summary>
    /// 是否3D显示
    /// </summary>
    public bool Is3DStyle
    {
        get { return _is3DStyle; }
        set { _is3DStyle = value; }
    }
    #endregion

    #region 标签颜色集合
    /// <summary>
    /// 标签颜色集合
    /// </summary>
    private List<Color> colorList = new List<Color>()
    {
        Color.FromArgb(20,144,206),
        Color.FromArgb(4,209,20),
        Color.FromArgb(176,222,9),
        Color.FromArgb(248,255,1),
        Color.FromArgb(255,158,1),
        Color.FromArgb(247,4,6),
        Color.FromArgb(8,8,8),
        Color.FromArgb(255,255,255)
    };
    #endregion

    #region 图表名称
    private string _imageName = Guid.NewGuid().ToString();
    /// <summary>
    /// 图表名称
    /// </summary>
    public string ImageName
    {
        get { return _imageName; }
        set { _imageName = value; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        DisplayChart();
    }

    /// <summary>
    /// 随机生成一种颜色
    /// </summary>
    /// <returns></returns>
    private Color GetRandomColor()
    {
        Random random = new Random();
        int r, g, b;
        r = random.Next(0, 255);
        g = random.Next(0, 255);
        b = random.Next(0, 255);
        return Color.FromArgb(r, g, b);
    }

    /// <summary>
    /// 生成图表
    /// </summary>
    private void DisplayChart()
    {
        //设置图形的基本属性
        ChartControl.Palette = ChartColorPalette.Dundas;
        ChartControl.Width = Unit.Pixel(Width);
        ChartControl.Height = Unit.Pixel(Height);
        ChartControl.BackGradientType = GradientType.TopBottom;
        ChartControl.BorderLineStyle = ChartDashStyle.Solid;
        ChartControl.BorderLineWidth = 0;
        ChartControl.BorderLineColor = Color.FromArgb(145, 202, 241);
        //ChartControl.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
        ChartControl.BorderSkin.FrameBorderWidth = 0;
        ChartControl.BackColor = Color.Transparent;
       // ChartControl.BackGradientEndColor = Color.FromArgb(170, 211, 231);
        ChartControl.AntiAliasing = AntiAliasing.All;
        ChartControl.ToolTip = Title;
        //ChartControl.RenderType = RenderType.ImageTag;
        ChartControl.ImageUrl = "~" + ConfigHelper.AppSettings("ChartImages") + ImageName;

        //设置标题属性
        ChartControl.Titles["Title"].Text = Title;
        ChartControl.Titles["Title"].Font = new System.Drawing.Font("Verdana", 8.0f);
        ChartControl.Titles["Title"].Color = Color.FromArgb(22, 72, 157);
        ChartControl.Titles["Title"].ShadowColor = Color.Silver;
        ChartControl.Titles["Title"].Position.Width = 94f;
        ChartControl.Titles["Title"].Position.Height = 18f;
        ChartControl.Titles["Title"].Position.X = 2f;
        ChartControl.Titles["Title"].Position.Y = 5f;
        //设置标签属性
        if (DtSource != null)
        {
            for (int i = 0; i < DtSource.Rows.Count; i++)
            {
                if (DtSource.Rows[i][Column1] != null && DtSource.Rows[i][Column2] != null && DtSource.Rows[i][Column1].ToString() != "合计")
                {
                    LegendItem legendItem = new LegendItem();
                    legendItem.Name = DtSource.Rows[i][Column1].ToString();
                    legendItem.ToolTip = DtSource.Rows[i][Column1].ToString();
                    legendItem.ShadowOffset = 1;
                    if (i >= colorList.Count)
                    {
                        legendItem.Color = GetRandomColor();
                    }
                    else
                    {
                        legendItem.Color = colorList[i];
                    }
                    legendItem.Style = LegendImageStyle.Marker;
                    legendItem.MarkerStyle = MarkerStyle.Square;
                    legendItem.MarkerSize = 10;
                    legendItem.BorderColor = Color.Transparent;
                    ChartControl.Legends["Default"].CustomItems.Add(legendItem);
                }
            }
        }
        ChartControl.Legends["Default"].BackColor = Color.Transparent;
        ChartControl.Legends["Default"].Alignment = StringAlignment.Center;
        ChartControl.Legends["Default"].BorderColor = Color.Transparent;
        ChartControl.Legends["Default"].Font = new Font("Verdana", 8.5f);
        ChartControl.Legends["Default"].FontColor = Color.FromArgb(22, 72, 157);
        ChartControl.Legends["Default"].LegendStyle = LegendStyle.Column;
        ChartControl.Legends["Default"].Docking = LegendDocking.Right;
        ChartControl.Legends["Default"].Position.X = 60f;
        ChartControl.Legends["Default"].Position.Y = 20f;
        ChartControl.Legends["Default"].Position.Width = 50f;
        ChartControl.Legends["Default"].Position.Height = 60f;
        //设置系列属性
        ChartControl.Series["Default"].Type = ChartType;
        //ChartControl.Series["Default"]["PieDrawingStyle"] = "SoftEdge";
        ChartControl.Series["Default"].Font = new System.Drawing.Font("Verdana", 8.25f);
        ChartControl.Series["Default"].FontColor = Color.White;
        ChartControl.Series["Default"].ShowLabelAsValue = true;
        ChartControl.Series["Default"].BorderColor = Color.Transparent;
        ChartControl.Series["Default"].ToolTip = "#VALX,#VAL";
        ChartControl.Series["Default"].ShowInLegend = false;
        ChartControl.Series["Default"].ShadowOffset = 2;
        ChartControl.Series["Default"].ShadowColor = Color.Gray;
       // ChartControl.AnimationTheme = AnimationTheme.GrowingAndFading;
        if (DtSource != null)
        {
            for (int i = 0; i < DtSource.Rows.Count; i++)
            {
                if (DtSource.Rows[i][Column1] != null && DtSource.Rows[i][Column2] != null)
                {
                    if (double.Parse(DtSource.Rows[i][Column2].ToString()) > 0 && DtSource.Rows[i][Column1].ToString()!="合计")
                    {
                        ChartControl.Series["Default"].Points.AddXY(DtSource.Rows[i][Column1], DtSource.Rows[i][Column2]);
                        if (i >= colorList.Count)
                        {
                            ChartControl.Series["Default"].Points[i].Color = GetRandomColor();
                        }
                        else
                        {
                            ChartControl.Series["Default"].Points[i].Color = colorList[i];
                        }
                    }
                }
            }
        }
        //设置区域属性
        ChartControl.ChartAreas["Default"].AxisX.LabelStyle.FontAngle = 30;
        ChartControl.ChartAreas["Default"].Position.Width = 55f;
        ChartControl.ChartAreas["Default"].Position.Height = 65f;
        ChartControl.ChartAreas["Default"].Position.X = 3f;
        ChartControl.ChartAreas["Default"].Position.Y = 20f;
        ChartControl.ChartAreas["Default"].BackColor = Color.Transparent;
        ChartControl.ChartAreas["Default"].BorderColor = Color.Gray;
        ChartControl.ChartAreas["Default"].Area3DStyle.Enable3D = Is3DStyle;

        //若没有数据，则显示空图
        if (ChartControl.Series["Default"].Points.Count <= 0)
        {
            //ChartControl.Legends["Default"].Enabled = false;
            ChartControl.Series["Default"].Points.AddXY("无数据", 1);
            ChartControl.Series["Default"].Points[0].Label = " ";
            ChartControl.Series["Default"].ToolTip = "暂无数据";
            ChartControl.Series["Default"].Points[0].Color = Color.FromArgb(151, 203, 240);
        }
    }

}
